package com.kma.summer2012.client.Autorisation;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.client.Header.Header;
import com.kma.summer2012.client.PasswordRecovery.PasswordRecovery;
import com.kma.summer2012.shared.model.UICrumbsUser;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 16.01.13
 * Time: 17:44
 * To change this template use File | Settings | File Templates.
 */
public class Authorisation extends Composite /*implements HasLoginHandler*/ {

   /* @Override
    public HandlerRegistration addLoginHandler(LoginHandler loginHandler) {
        return addHandler(loginHandler,LoginEvent.getType());
    }  */

    interface AuthorisationUiBinder extends UiBinder<Widget, Authorisation> {
    }

    private static AuthorisationUiBinder ourUiBinder = GWT.create(AuthorisationUiBinder.class);

    @UiField
    protected Label loginText;

    @UiField
    protected Label passwordText;

    @UiField
    protected TextBox loginBox;

    @UiField
    protected PasswordTextBox passwordBox;

    @UiField
    protected Button loginButton;
    
    @UiField
    protected CheckBox showPassword;

    @UiField
    protected Label wrongPassword;
    
    @UiField
    protected Anchor passRecovery;

    public Authorisation() {
        HTMLPanel rootElement = (HTMLPanel) ourUiBinder.createAndBindUi(this);
        initWidget(rootElement);
        RootPanel.get().add(this);
        this.loginButton.setText("Login");
        this.setLoginText("Login:");
        this.setPasswordText("Password:");
        this.passRecovery.setText("Forgot your password?");
        CrumbsEventBus.getInstance().addHandler(LoginEvent.getType(), new LoginEvent.LoginEventHandler() {
            @Override
            public void onLogin() {
                CrumbsAuthorisationService.App.getInstance().getByLoginPassword(Authorisation.this.loginBox.getText(), Authorisation.this.passwordBox.getText(), new AsyncCallback<UICrumbsUser>() {
                    public void onFailure(Throwable caught) {
                        Authorisation.this.wrongPassword.setText("Invalid password or login!");
                    }

                    public void onSuccess(UICrumbsUser result) {
                        //Header h=new Header(result);
                        Authorisation.this.loginBox.setValue("");
                        Authorisation.this.passwordBox.setValue("");
                        RootPanel.get().remove(Authorisation.this);
                        RootPanel.get().clear();
                        RootPanel.get().add(new Header(result));
                        
                    }
            } );
            }
        });
        this.showPassword.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

            @Override
            public void onValueChange(ValueChangeEvent<Boolean> event) {
                if(event.getValue()) {
                    Authorisation.this.passwordBox.getElement().setAttribute("type", "text");
                }
                else{
                    Authorisation.this.passwordBox.getElement().setAttribute("type", "password");
                }
            }
        });

        this.loginButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                CrumbsEventBus.getInstance().fireEvent(new LoginEvent());
            }
        });
        this.passRecovery.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                Authorisation.this.loginBox.setValue("");
                Authorisation.this.passwordBox.setValue("");
                RootPanel.get().remove(Authorisation.this);
                RootPanel.get().clear();
                RootPanel.get().add(new PasswordRecovery());
            }
        });


}

    public void setLoginText(String loginText) {
        this.loginText.setText(loginText);
    }

    public void setPasswordText(String passwordText) {
        this.passwordText.setText(passwordText);
    }



}